<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      button {
        width: 90px;
        height: 40px;
      }
      #father {
        border: 1px solid red;
        width: 100px;
        overflow: hidden;
        transition: all 0.6s;
      }
      .son {
        padding: 10px;
      }
    </style>
  </head>
  <body>
    <button id="btn">点一下！</button>
    <div id="father">
      <div id="son" class="son">
        内容内容内容内容内容内容内容 内容内容内容内容内容内容内容
        内容内容内容内容内容内容内容 内容内容内容内容内容内容内容
        内容内容内容内容内容内容内容
      </div>
    </div>
    <script>
      const btn = document.getElementById('btn')
      const father = document.getElementById('father')
      const son = document.getElementById('son')
      let isShow = false

      // 预设一下，不然都是0，没动画
      const offerHight = son.offsetHeight + 'px'
      father.style.height = offerHight

      btn.addEventListener('click', () => {
        if (isShow) {
          father.style.display = 'block'
          const offerHight = son.offsetHeight + 'px'
          father.style.height = offerHight
        } else {
          father.style.height = 0
          father.addEventListener('transitionend', () => {
            if (son.offsetHeight === 0) father.style.display = 'none'
          })
        }
        isShow = !isShow
      })
    </script>
  </body>
</html>
